/**
 * 
 */
package com.tidux.app.tiduxbooker.common;

import java.io.File;
import java.io.IOException;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.ImageView;

/**
 * @author ren.tangr
 * 
 */
public class CameraPreview extends SurfaceView implements
		SurfaceHolder.Callback {
	SurfaceHolder mHolder;
	Camera mCamera;

	public CameraPreview(Context context) {
		super(context);

		// Install a SurfaceHolder.Callback so we get notified when the
		// underlying surface is created and destroyed.
		mHolder = getHolder();
		mHolder.addCallback(this);
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// The Surface has been created, acquire the camera and tell it where
		// to draw.
		mCamera = Camera.open();
		try {
			mCamera.setPreviewDisplay(holder);
		} catch (IOException exception) {
			// TODO: add more exception handling logic here
			mCamera.release();
			mCamera = null;
		} finally {

		}
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// Surface will be destroyed when we return, so stop the preview.
		// Because the CameraDevice object is not a shared resource, it's very
		// important to release it when the activity is paused.
		mCamera.stopPreview();
		mCamera.release();
		mCamera = null;
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		// Now that the size is known, set up the camera parameters and begin
		// the preview.
		Camera.Parameters parameters = mCamera.getParameters();

		List<Size> sizes = parameters.getSupportedPreviewSizes();
		parameters.setPreviewSize(w, h);

		mCamera.setParameters(parameters);
		mCamera.startPreview();
	}

	/**
	 * @return the mHolder
	 */
	public SurfaceHolder getmHolder() {
		return mHolder;
	}

	/**
	 * @param mHolder the mHolder to set
	 */
	public void setmHolder(SurfaceHolder mHolder) {
		this.mHolder = mHolder;
	}

	/**
	 * @return the mCamera
	 */
	public Camera getmCamera() {
		return mCamera;
	}

	/**
	 * @param mCamera the mCamera to set
	 */
	public void setmCamera(Camera mCamera) {
		this.mCamera = mCamera;
	}

}